﻿Imports System.Text

Delegate Sub AddControl(ByVal hWnd As IntPtr)
Delegate Sub RemoveControl(ByVal hWnd As IntPtr)
Delegate Sub SetWidth()

Public Class Taskbar
    Dim WindowList As New Dictionary(Of IntPtr, WindowControl)
    Dim newgw As New List(Of IntPtr)

    Private Sub UserControl_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Me.Background = New LinearGradientBrush(Color.FromArgb(255, 40, 40, 40), Colors.Black, 90)
        Dim t As New System.Threading.Thread(AddressOf UpdateWindowsLoop)
        t.Start()
    End Sub

    Private Sub UpdateWindows()
        Dim rc As New RemoveControl(AddressOf RemoveControl)
        Dim ac As New AddControl(AddressOf AddControl)
        Dim sw As New SetWidth(AddressOf SetWidth)
        Dim gw As List(Of IntPtr) = GetWindows()


        For Each h As IntPtr In gw
            If Not WindowList.ContainsKey(h) Then
                If IsAltTabWindow(h) Then
                    StackPanelMain.Dispatcher.Invoke(ac, h)
                End If
            End If
        Next
        Dim DelList As New List(Of IntPtr)
        For Each h As IntPtr In WindowList.Keys
            If Not gw.Contains(h) Then
                DelList.Add(h)
            End If
        Next
        For Each h As IntPtr In DelList
            Me.StackPanelMain.Dispatcher.Invoke(rc, h)
        Next
    End Sub

    Public Function IsAltTabWindow(ByVal Window As IntPtr) As Boolean
        If (Window = GetShellWindow()) Then Return False
        Dim root As IntPtr = GetAncestor(Window, 3)

        If (GetLastVisibleActivePopUpOfWindow(root) = Window) Then


            Dim cn As New StringBuilder(255)
            GetClassName(Window, cn, 255)
            Dim ClassName As String = cn.ToString
            Dim wt As New StringBuilder(255)
            GetWindowText(Window, wt, 255)
            Dim WindowText As String = wt.ToString
            If WindowText = "" Or WindowText = "WinPanel" Then
                Return False
            End If

            If (ClassName = "Shell_TrayWnd" Or
                ClassName = "DV2ControlHost" Or
                ClassName = "MsgrIMEWindowClass" Or
                ClassName = "SysShadow" Or
                ClassName.StartsWith("WMP9MediaBarFlyout")) Then Return False

            Return True
        End If
        Return False

    End Function

    Private Sub UpdateWindowsLoop()
        Do
            UpdateWindows()
            System.Threading.Thread.Sleep(100)
        Loop
    End Sub


    Private Function GetLastVisibleActivePopUpOfWindow(ByVal window As IntPtr) As IntPtr
        Dim lastPopUp As IntPtr = GetLastActivePopup(window)
        If (IsWindowVisible(lastPopUp)) Then
            Return lastPopUp
        ElseIf (lastPopUp = window) Then
            Return IntPtr.Zero
        Else
            Return GetLastVisibleActivePopUpOfWindow(lastPopUp)
        End If
    End Function


    Sub AddControl(ByVal hWnd As IntPtr)
        Dim wc As New WindowControl
        wc.Width = 185
        wc.Height = 24
        wc.WinHandle = hWnd
        StackPanelMain.Children.Add(wc)
        WindowList.Add(hWnd, wc)
    End Sub

    Sub RemoveControl(ByVal hWnd As IntPtr)
        StackPanelMain.Children.Remove(WindowList(hWnd))
        WindowList.Remove(hWnd)
    End Sub

    Sub SetWidth()
        If WindowList.Count * 185 > My.Computer.Screen.Bounds.Width Then
            Dim NewWidth As Integer = My.Computer.Screen.Bounds.Width / WindowList.Count
            For Each wc As WindowControl In WindowList.Values
                wc.Width = NewWidth
            Next
        End If
    End Sub

End Class
